package com.huayi.datagovernance.utils.http;

import com.alibaba.fastjson.JSONObject;
import com.huayi.datagovernance.utils.RestTemplateUtils;
import com.huayi.datagovernance.vo.CommonResult;
import common.parent.system.web.ResponseResult;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * restTemplate请求示例
 * @author wuch
 * @version 1.0
 * @date 2021/4/14 11:42
 */
@RunWith(SpringRunner.class)
@SpringBootTest
public class RestTemplateDemo {

    /**
     * get请求无参
     */
    @Test
    public void test1(){
        CommonResult<JSONObject> result = RestTemplateUtils.getNoParam("http://localhost:8001/eduservice/teacher/findAll",JSONObject.class);
        System.out.println(result.getStatus());
        System.out.println(result.getMessage());
        System.out.println(result.getData());
    }

    /**
     * get请求-url参数
     */
    @Test
    public void test2(){
        Map<String,Object> map = new LinkedHashMap<>();
//        map.put("page",1);
//        map.put("size",3);
        map.put("courseId","1356067071291240450");
        CommonResult<JSONObject> result = RestTemplateUtils.getWithUrlParam("http://localhost:8001/eduservice/commentfront/getCommentList/1/3",JSONObject.class,map);
        System.out.println(result.getStatus());
        System.out.println(result.getMessage());
        System.out.println(result.getData());

        //不带参数
        CommonResult<JSONObject> result1 = RestTemplateUtils.getWithUrlParam("http://localhost:8001/eduservice/teacher/findAll",JSONObject.class,null);
        System.out.println(result1.getStatus());
        System.out.println(result1.getMessage());
        System.out.println(result1.getData());

    }

    /**
     * get请求-自定义请求头-请求参数
     */
    @Test
    public void test3(){
        //form
        Map<String,Object> map = new LinkedHashMap<>();
        map.put("courseId","1356067071291240450");
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        CommonResult<ResponseEntity<JSONObject>> result1 = RestTemplateUtils.getUrlParamAndHeader("http://localhost:8001/eduservice/commentfront/getCommentList/1/3",
                headers, JSONObject.class, map);
        System.out.println(result1.getData().getStatusCode());
        System.out.println(result1.getData().getBody());

        CommonResult<ResponseEntity<JSONObject>> result2 = RestTemplateUtils.getUrlParamAndHeader("http://localhost:8001/eduservice/teacher/findAll",
                headers, JSONObject.class, null);
        System.out.println(result2.getData().getStatusCode());
        System.out.println(result2.getData().getBody());
    }

    /**
     * post请求（支持url和json）
     */
    @Test
    public void test4(){
        Map<String,Object> map = new LinkedHashMap<>();
//        map.put("courseId","1356067071291240450");
        JSONObject json = new JSONObject();
        json.put("level",1);
        json.put("name","王");
//        HttpHeaders headers = new HttpHeaders();
//        headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
        CommonResult<JSONObject> result = RestTemplateUtils.postJson("http://localhost:8001/eduservice/teacher/pageTeacherCondition/1/3",
                JSONObject.class, map, json);
        System.out.println(result.getStatus());
        System.out.println(result.getMessage());
        System.out.println(result.getData());
    }

    /**
     * put请求
     */
    @Test
    public void test5(){
        String url = "http://localhost:8001/eduservice/chapter/updateChapter";
        JSONObject json = new JSONObject();
        json.put("courseId","1346763363145973762");
        json.put("id","1348580315794616321");
        json.put("sort",2);
        json.put("title","娃哈哈2");
        CommonResult<String> result = RestTemplateUtils.put(url, new HashMap<>(0), json);
        System.out.println(result.getStatus());
        System.out.println(result.getMessage());
        System.out.println(result.getData());
    }

    /**
     * delete请求
     */
    @Test
    public void test6(){
        String url = "http://localhost:8001/eduservice/chapter/{chapterId}";
        JSONObject json = new JSONObject();
        json.put("chapterId","4454");
        RestTemplateUtils.restTemplate.delete(url, json);
    }
}
